package xing.infrastructure.util;

import java.util.Random;
/********************************
 * 文 件 名 :StringUtil.java
 * 创 建 人：陈天龙
 * 日    期：2017/8/28
 * 描   述：字符串工具类
 * 版 本 号：v1.0.0
 ********************************/
public class StringUtil {

    static long seed = System.currentTimeMillis();
    static long skip = Long.parseLong("187649984473770");
    static char ch[]={'0','1','2','3','4','5','6','7','8','9'
            ,'A','B','C','D','E','F','G','H','I','J','K','L',
            'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
            'o','p','q','r','s','t','u','v','w','x','y','z',};

    /**
     * 描    述：随机6位字符串
     * 创 建 人：陈天龙
     * 日    期：2017/8/28
     * @param length 需要返回的字符串长度
     * @return String
     */
    public static String getString(int length){
        if(length>62 || length<=0){
            throw new IllegalArgumentException();
        }else if(length == 62){
            length--;
        }

        Random r = new Random(seed);
        int rNum;
        char temp;
        for(int i=0; i<length; i++){
            rNum = r.nextInt(62);
            seed += skip;
            r.setSeed(seed);
            if(rNum < i){
                rNum = i+1;
            }
            temp = ch[i];
            ch[i] = ch[rNum];
            ch[rNum] = temp;
        }
        return new String(ch,0,length);
    }
}
